package com.memory;

/**
 *
 * @author kennux
 */

import com.cpu.z80.register.Double8Bit;

public class Memory {
     byte[] memory;

     public Memory(short memorysize) {
         memory = new byte[memorysize];
     }

     public byte get8Bit(short address) {
         try {
            return memory[address];
         } catch (java.lang.ArrayIndexOutOfBoundsException e) {
             System.out.println("Memory.java - get8Bit() Exception!");
             System.out.println("Tried to read memory adress " + address);
             System.out.println("Not mapped to the ram-emulation! Returning 0");
             return (char)0;
         } catch (Exception e) {
             System.out.println("Memory.java - get8Bit() Exception!");
             System.out.println("Unkown error. Adress: " + address);
             System.out.println("StackTrace:");
             e.printStackTrace();
             System.out.println("Returning 0");
             return (char)0;
         }
     }

     public void set8Bit(short address, byte value) {
         try {
            memory[address] = value;
         } catch (java.lang.ArrayIndexOutOfBoundsException e) {
             System.out.println("Memory.java - set8Bit() Exception!");
             System.out.println("Tried to write " + (int)value + " to memory adress " + address);
             System.out.println("Not mapped to the ram-emulation!");
         } catch (Exception e) {
             System.out.println("Memory.java - set8Bit() Exception!");
             System.out.println("Unkown error. Adress: " + address);
             System.out.println("StackTrace:");
             e.printStackTrace();
         }
     }

     public short get16Bit(short address) {
         try {
            return (short)((memory[address] << 8) | memory[address + 1]);
         } catch (java.lang.ArrayIndexOutOfBoundsException e) {
             System.out.println("Memory.java - get16Bit() Exception!");
             System.out.println("Tried to read memory adress " + address);
             System.out.println("Not mapped to the ram-emulation! Returning 0");
             return (char)0;
         } catch (Exception e) {
             System.out.println("Memory.java - get16Bit() Exception!");
             System.out.println("Unkown error. Adress: " + address);
             System.out.println("StackTrace:");
             e.printStackTrace();
             System.out.println("Returning 0");
             return (char)0;
         }
     }

     public void set16Bit(short address, short value) {
         try {
            memory[address] = (byte)(value >> 8);
            memory[address+1] = (byte)(value & 0xFF);
         } catch (java.lang.ArrayIndexOutOfBoundsException e) {
             System.out.println("Memory.java - set16Bit() Exception!");
             System.out.println("Tried to write " + (int)value + " to memory adress " + address);
             System.out.println("Not mapped to the ram-emulation!");
         } catch (Exception e) {
             System.out.println("Memory.java - set16Bit() Exception!");
             System.out.println("Unkown error. Adress: " + address);
             System.out.println("StackTrace:");
             e.printStackTrace();
         }
     }
}
